package cn.iocoder.yudao.module.custom.controller.admin.province;

import cn.iocoder.yudao.module.custom.convert.industry.IndustryConvert;
import cn.iocoder.yudao.module.custom.dal.dataobject.industry.IndustryDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.custom.controller.admin.province.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.province.ProvinceDO;
import cn.iocoder.yudao.module.custom.convert.province.ProvinceConvert;
import cn.iocoder.yudao.module.custom.service.province.ProvinceService;

/**
 * @author Administrator
 */
@Tag(name = "管理后台 - 省级信息")
@RestController
@RequestMapping("/custom/province")
@Validated
public class ProvinceController {

    @Resource
    private ProvinceService provinceService;

    @PostMapping("/create")
    @Operation(summary = "创建省级信息")
    @PreAuthorize("@ss.hasPermission('custom:province:create')")
    public CommonResult<String> createProvince(@Valid @RequestBody ProvinceCreateReqVO createReqVO) {
        return success(provinceService.createProvince(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新省级信息")
    @PreAuthorize("@ss.hasPermission('custom:province:update')")
    public CommonResult<Boolean> updateProvince(@Valid @RequestBody ProvinceUpdateReqVO updateReqVO) {
        provinceService.updateProvince(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除省级信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('custom:province:delete')")
    public CommonResult<Boolean> deleteProvince(@RequestParam("id") String id) {
        provinceService.deleteProvince(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得省级信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('custom:province:query')")
    public CommonResult<ProvinceRespVO> getProvince(@RequestParam("id") String id) {
        ProvinceDO province = provinceService.getProvince(id);
        System.out.println(province);
        return success(ProvinceConvert.INSTANCE.convert(province));
    }



    @GetMapping("/list")
    @Operation(summary = "获得省级信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('custom:province:query')")
    public CommonResult<List<ProvinceRespVO>> getProvinceList(@RequestParam("ids") Collection<String> ids) {
        List<ProvinceDO> list = provinceService.getProvinceList(ids);
        return success(ProvinceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得省级信息分页")
    @PreAuthorize("@ss.hasPermission('custom:province:query')")
    public CommonResult<PageResult<ProvinceRespVO>> getProvincePage(@Valid ProvincePageReqVO pageVO) {
        PageResult<ProvinceDO> pageResult = provinceService.getProvincePage(pageVO);
        return success(ProvinceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出省级信息 Excel")
    @PreAuthorize("@ss.hasPermission('custom:province:export')")
    @OperateLog(type = EXPORT)
    public void exportProvinceExcel(@Valid ProvinceExportReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<ProvinceDO> list = provinceService.getProvinceList(exportReqVO);
        // 导出 Excel
        List<ProvinceExcelVO> datas = ProvinceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "省级信息.xls", "数据", ProvinceExcelVO.class, datas);
    }

}
